package com.protocols.sctp.m3ua.sccp.tcap.map;

import java.io.Serializable;

import com.protocols.BCDNumber;
import com.protocols.GeneralProtocolData;
import com.protocols.OctaveInteger;
import com.protocols.ProtoOperations;
import com.protocols.ProtocolStackException;
import com.protocols.sctp.m3ua.sccp.tcap.TcapBegin;
import com.protocols.sctp.m3ua.sccp.tcap.TcapConstructor;
import com.protocols.sctp.m3ua.sccp.tcap.TcapPrimitiveUnitByteArray;
import com.protocols.sctp.m3ua.sccp.tcap.TcapPrimitiveUnitNumber;

public class MapOpen implements GeneralProtocolData,Serializable{

	protected TcapConstructor MapOpen = new TcapConstructor(0xa0);
	protected TcapPrimitiveUnitByteArray DestinationReference = new TcapPrimitiveUnitByteArray(0x80);
	protected TcapPrimitiveUnitByteArray OriginationReference = new TcapPrimitiveUnitByteArray(0x81);
	protected TcapPrimitiveUnitByteArray Extention = new TcapPrimitiveUnitByteArray(0x30);
	
	public MapOpen() {
		
		MapOpen.addElement(DestinationReference);
		MapOpen.addElement(OriginationReference);
		MapOpen.addElement(Extention);		
	}
	
	public byte [] getByteArray(){	
		return MapOpen.getByteArray();

	}	

	public TcapConstructor getRootComponent(){
		return MapOpen;
	}
	
	public int getDestinationReferenceNumberInfo() {
		return ProtoOperations.intFromByteArray(DestinationReference.getTagValue(), 0, 1);
	}
	
	
	public String getDestinationReferenceNumber() {
		
		byte [] val = DestinationReference.getTagValue();
		
		if (val.length>1){
			int i=1;
			String value="";
			while (i<val.length){
				value+=(val[i] & 0b00001111);
				value+=(val[i] & 0b11110000)>>4;
				i++;			
			}			
			return value;
		}
			
		
		return "";
	}

	public void setDestinationReference(int destinationReferenceInfo,String destinationReferenceNumber) {
		BCDNumber destref = new BCDNumber(destinationReferenceNumber, "f");
		
		DestinationReference.setTagValue(ProtoOperations.concatArrays(new byte []{(byte)destinationReferenceInfo}, destref.getByteArray()));
	}
	
	
	public int getOriginationReferenceNumberInfo() {
		return ProtoOperations.intFromByteArray(OriginationReference.getTagValue(), 0, 1);
	}
	
	
	public String getOriginationReferenceNumber() {
		
		byte [] val = OriginationReference.getTagValue();
		
		if (val.length>1){
			int i=1;
			String value="";
			while (i<val.length){
				value+=(val[i] & 0b00001111);
				value+=(val[i] & 0b11110000)>>4;
				i++;			
			}			
			return value;
		}
			
		
		return "";
	}

	public void setOriginationReference(int originationReferenceInfo,String originationReferenceNumber) {
		BCDNumber origref = new BCDNumber(originationReferenceNumber, "f");
		
		OriginationReference.setTagValue(ProtoOperations.concatArrays(new byte []{(byte)originationReferenceInfo}, origref.getByteArray()));
	}
	
	
	

	@Override
	public int getLength() {
		return getByteArray().length;
	}

	@Override
	public byte[] decode(byte[] input) throws ProtocolStackException {
		
		return MapOpen.decode(input);
	}
	
	
}
